﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace E1_eMart
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["PreviousPage"] = Request.UrlReferrer;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext edata = new DataClasses1DataContext();
            if (Request.Params["username"] == null || Request.Params["password"] == null)
                return;
            String username = Request.Params["username"].Trim();
            String password = Request.Params["password"].Trim();

            //var _taikhoan = from tk in edata.TaiKhoans
            //                where tk.TenTaiKhoan == username && tk.MatKhau == password
            //                select tk;
            TaiKhoan taikhoan = edata.TaiKhoans.Single<TaiKhoan>(tk => tk.MatKhau == password && tk.TenTaiKhoan == username);
            //if (_taikhoan == null)
            //    return;

            //if (_taikhoan.Count() <= 0)
            //    return;

            //TaiKhoan taikhoan = _taikhoan.ToList()[0];

            if (Session["username"] == null)
            {
                Session.Add("username", taikhoan);
            }
            if (Session["username"] != null)
                if (ViewState["PreviousPage"] != null)
                {
                    Response.Redirect(ViewState["PreviousPage"].ToString());
                }
                
        }
    }
}